package top.infopub.mgr.bpa.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.bpa.PrimarySureDto;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.OutcaeSureAttDao;
import top.infopub.mgr.bpa.dao.OutcaeSureDao;
import top.infopub.mgr.bpa.dao.OutcaeSureDetailDao;
import top.infopub.mgr.bpa.dao.OutcaeSureSubDao;
import top.infopub.mgr.bpa.domain.OutcaeSureDomain;
import top.infopub.mgr.bpa.service.OutcaeSureService;
import top.infopub.mgr.bpa.vo.OutcaeSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.ReflectBeanUtils;
import top.infopub.model.bpa.OutcaeSure;
import top.infopub.model.bpa.OutcaeSureAtt;
import top.infopub.model.bpa.OutcaeSureDetail;
import top.infopub.model.bpa.OutcaeSureSub;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OutcaeSureServiceImpl extends GenericServiceImpl<OutcaeSure, String> implements
		OutcaeSureService {

    @Autowired
    private OutcaeSureDao outcaeSureDao;
    
    @Autowired
    private OutcaeSureSubDao outcaeSureSubDao;
    
    @Autowired
    private OutcaeSureDetailDao outcaeSureDetailDao;
    
    @Autowired
    private OutcaeSureAttDao outcaeSureAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;


    @Override
    public GenericDao<OutcaeSure, String> getDao() {
        return outcaeSureDao;
    }

    @Override
    public Page<OutcaeSureDomain> selectByDomainWhere(Page<OutcaeSureDomain> page,
                                                      OutcaeSureVo outcaeSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OutcaeSureDomain>)outcaeSureDao.selectByDomainWhere(outcaeSureVo);
    }

    @Override
    public Page<OutcaeSureDomain> selectMyCheckByDomainWhere(Page<OutcaeSureDomain> page,
                                                             OutcaeSureVo outcaeSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OutcaeSureDomain>)outcaeSureDao.selectMyCheckByDomainWhere(outcaeSureVo);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveOutcaeSure(OutcaeSure outcaeSure, List<OutcaeSureSub> outcaeSureSubs,
                               List<OutcaeSureDetail> outcaeSureDetails,
                               List<OutcaeSureAtt> outcaeSureAtts) {
        //主表操作
        outcaeSureDao.insertById(outcaeSure);

        if(CollectionUtils.isNotEmpty(outcaeSureSubs)){
            outcaeSureSubDao.insertByBatch(outcaeSureSubs);
        }
        
        if(CollectionUtils.isNotEmpty(outcaeSureDetails)){
            outcaeSureDetailDao.insertByBatch(outcaeSureDetails);
        }
        
        if(CollectionUtils.isNotEmpty(outcaeSureAtts)){
            outcaeSureAttDao.insertByBatch(outcaeSureAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_OUTCAE_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, outcaeSure.getId(), outcaeSure.getProcessId(), null);
        
    }

    @Override
    public BigDecimal selectBySure(Map<String, Object> map) {
        return outcaeSureDao.selectBySure(map);
    }

    @Override
    public BigDecimal selectDecimalByProCode(Map<String, Object> map) {
        return outcaeSureDao.selectDecimalByProCode(map);
    }

    @Override
    public OutcaeSureVo queryById(String id) {
        return outcaeSureDao.queryById(id);
    }

    @Override
    public String insertById(PrimarySureDto primarySureDto, String creator) {
        OutcaeSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, OutcaeSureVo.class);
        Date date = new Date();
        sureVo.setAddBy(creator);
        sureVo.setAddTime(date);
        sureVo.setUptBy(creator);
        sureVo.setUptTime(date);
        outcaeSureDao.insertSelective(sureVo);
        return sureVo.getId();
    }

    @Override
    public void updateByIdSelective(PrimarySureDto primarySureDto, String modifier) {
        OutcaeSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, OutcaeSureVo.class);
        sureVo.setId(primarySureDto.getId());
        Date date = new Date();
        sureVo.setUptBy(modifier);
        sureVo.setUptTime(date);
        outcaeSureDao.updateByIdSelective(sureVo);
    }


}